//
//  NodeTest.m
//  RunLoopDemo
//
//  Created by zhangjikuan on 2020/11/9.
//  Copyright © 2020 hsgd. All rights reserved.
//

#import "NodeTest.h"

@implementation NodeTest

+ (void)printNode:(Node *)node {
    if (node) {
        NSLog(@"%ld",node.value);
        if (node.next) {
            [self printNode:node.next];
        }
    }
}

+ (Node *)reversalNode:(Node *)head {
    [self printNode:head];
    
    if (head == nil || head.next == nil) {
        return head;
    }
    
    // 核心算法 就地反转法
    Node *dummy = [[Node alloc] init];
    dummy.value = -1;
    dummy.next = head;
    Node *prev = dummy.next;
    Node *pCur = prev.next;
    
    while (pCur) {
        prev.next = pCur.next;
        pCur.next = dummy.next;
        dummy.next = pCur;
        pCur = prev.next;
    }
    
    [self printNode:dummy.next];
    
    
    // 核心算法 头结点插入法
    Node *head2 = dummy.next;
    Node *dummy2 = [[Node alloc] init];
    dummy2.value = -1;
    Node *pCur2 = head2;
    
    while (pCur2) {
        Node *pNex = pCur2.next;
        pCur2.next = dummy2.next;
        dummy2.next = pCur2;
        pCur2 = pNex;
    }
    [self printNode:dummy2.next];
    
    return dummy.next;

}


@end
